/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using IQIGame.Onigao.Framework;
using System.Collections.Generic;
using System.Linq;
using System;
using NetProtocol.Enum;

namespace NetProtocol.POD {

	/// <summary>
	///  单个编队数据，如果做助战，还会下发助战的英雄信息
	/// module : structs
	/// </summary>
	public class FormationPOD : IPOD 
    {
        public readonly static Func<FormationPOD> FACTORY = () => new FormationPOD();
		public static Func<FormationPOD> DynamicFactory = FACTORY;
        
		/*-- members --*/
		/// <summary>
        ///  编队序号
        /// </summary>
		public int formId;
		/// <summary>
        ///  编队名字，空表默认
        /// </summary>
		public string formName;
		/// <summary>
        ///  队长英雄cid
        /// </summary>
		public int captainHeroCid;
		/// <summary>
        ///  上阵英雄信息 -- 所有位置，pos->heroId
        /// </summary>
        private Dictionary<int, FormationHeroPOD> m_heroes;
		
		
        public Dictionary<int, FormationHeroPOD> heroes
        {
            get
            {
                if(m_heroes == null)
                {
                    m_heroes = new();
                }
                return m_heroes;
            }
            private set => m_heroes = value;
        }
        public Dictionary<int, FormationHeroPOD> unsafe_heroes
        {
        	get => m_heroes;
        	set => m_heroes = value;
        }
		
		/*-- constructors --*/
		public FormationPOD() {}
		public FormationPOD(int formId, string formName, int captainHeroCid, Dictionary<int, FormationHeroPOD> heroes) {
			this.formId = formId;
			this.formName = formName;
			this.captainHeroCid = captainHeroCid;
			this.m_heroes = heroes;
		}
		  public void Serial(SOutputStream _out) {
                if(formId != default) {
                    _out.WriteByte(1);
                    _out.Write_int(formId);
                }
                if(formName != default) {
                    _out.WriteByte(2);
                    _out.Write_string(formName);
                }
                if(captainHeroCid != default) {
                    _out.WriteByte(3);
                    _out.Write_int(captainHeroCid);
                }
                if(m_heroes != default) {
                    _out.WriteByte(4);
                    _out.Write_map_int_pod(m_heroes);
                }
        }
            
        public void UnSerial(SInputStream _in) {
            while (_in.HasRemaining())
            {
                int _key = _in.ReadByte();
                switch (_key)
                {
                    case 1:
                        formId = _in.Read_int();
                        break;
                    case 2:
                        formName = _in.Read_string();
                        break;
                    case 3:
                        captainHeroCid = _in.Read_int();
                        break;
                    case 4:
                        m_heroes = _in.Read_map_int_pod<FormationHeroPOD>(FormationHeroPOD.DynamicFactory);
                        break;
                    default: _in.SkipBinary();break;
                }
            }
        }
		
		public FormationPOD Clone() {
            FormationPOD o = DynamicFactory();
            o.formId = formId;
            o.formName = formName;
            o.captainHeroCid = captainHeroCid;
            if (m_heroes != null) {
                o.m_heroes = new ();
				m_heroes.ToList().ForEach(kv => o.m_heroes.Add(kv.Key, kv.Value?.Clone()));
            }
            return o;
        }
        
        public void Clear() {
            formId = default;
            formName = default;
            captainHeroCid = default;
            m_heroes = default;
        }
		
        public override string ToString() {
			string str = "";
			str += "FormationPOD [";
			str += "formId=";
			str += formId;
			str += ", formName=";
			str += formName;
			str += ", captainHeroCid=";
			str += captainHeroCid;
			str += ", heroes=";
			str += m_heroes.ToString<int, FormationHeroPOD>();
			str += "]";
			return str;
		}
		
	}
}